﻿using Newtonsoft.Json;
using PlaceOrder_Core.Helper;
using PlaceOrder_Core.Services.Instances;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;

namespace PlaceOrder_Core.Services.Wechat
{
    public class WeChatService
    {
        private static readonly HttpClient httpClient = new HttpClient();

        public static async Task SendWeChatNotificationAsync(WeChatNotificationClass notification)
        {
            try
            {
                notification.MessageType = WeChatNotificationClass.WeChatNotificationMessageType.Upgrade;
                // 设置请求头信息
                httpClient.DefaultRequestHeaders.Accept.Clear();
                httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                httpClient.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.75 Safari/537.36 Edg/100.0.1185.36");
                httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", UserHelper.Token);

                // 设置请求的URL
                string url = $"{DataHelper.BaseConfig.HttpUrl}/api/userlevelchange";

                // 构建JSON数据
                var jsonContent = new StringContent(JsonConvert.SerializeObject(new
                {
                    userId = notification.UserID,
                    thing1 = notification.NickName,
                    time7 = DateTime.Now.ToString("yyyy-MM-dd"),
                    amount4 = notification.TotalProfit,
                    thing12 = notification.Message
                }), Encoding.UTF8, "application/json");

                // 发送请求并获取响应
                HttpResponseMessage response = await httpClient.PostAsync(url, jsonContent);

                // 检查响应状态
                if (!response.IsSuccessStatusCode)
                {
                    string errorContent = await response.Content.ReadAsStringAsync();
                    AppLog.WriteError($"{response.ReasonPhrase}: {errorContent}");
                }
                else
                {
                    string responseBody = await response.Content.ReadAsStringAsync();
                    // 处理响应内容
                    AppLog.WriteInfo($"发送消息成功。ID:{notification.UserID},消息内容：{notification.Message}");
                }
            }
            catch (Exception ex)
            {
                // 记录异常
                AppLog.WriteError($"Exception: {ex.Message}");
            }
        }
    }




    public class WeChatNotificationClass
    {
        public enum WeChatNotificationMessageType
        {
            Upgrade,
            Forbid
        }
        public int UserID { get; set; }
        public string NickName { get; set; }
        public decimal TotalProfit { get; set; }
        public WeChatNotificationMessageType MessageType { get; set; }
        public string Message { get; set; }
    }
}
